#!/bin/bash

Install_memc()
{
    Nginx_Version=`/usr/local/nginx/sbin/nginx -v 2>&1 | cut -c22-`

    if [ -s /usr/local/include/jemalloc/jemalloc.h ] && /usr/local/nginx/sbin/nginx -V 2>&1|grep -Eqi 'ljemalloc'; then
        NginxMAOpt="--with-ld-opt='-ljemalloc'"
     elif [ -s /usr/local/include/gperftools/tcmalloc.h ] && grep -Eqi "google_perftools_profiles" /usr/local/nginx/conf/nginx.conf; then
        NginxMAOpt='--with-google_perftools_module'
     else
        NginxMAOpt=""
    fi
    Nginx_Modules_Options="${Nginx_Modules_Options} --add-module=../memc-nginx-module-master --add-module=../srcache-nginx-module-master"
    echo "You will install nginx with memc-nginx-module ..."
    Press_Start
    cd ${cur_dir}/src
    if [ -s nginx-${Nginx_Version}.tar.gz ]; then
        echo "nginx-${Nginx_Version}.tar.gz [found]"
     else
        echo "Error: nginx-${Nginx_Version}.tar.gz not found!!!download now......"
        wget -c ${Download_Mirror}/nginx/nginx-${Nginx_Version}.tar.gz
        if [ $? -eq 0 ]; then
            echo "Download nginx-${Nginx_Version}.tar.gz successfully!"
         else
            wget -c http://nginx.org/download/nginx-${Nginx_Version}.tar.gz
            if [ $? -eq 0 ]; then
                echo "Download nginx-${Nginx_Version}.tar.gz successfully!"
             else
                echo "You enter Nginx Version was:"${Nginx_Version}
                Echo_Red "Error! You entered a wrong version number, please check!"
                sleep 5
                exit 1
            fi
        fi
    fi
    Download_Files ${Download_Mirror}/nginx/srcache-nginx-module-master.zip srcache-nginx-module-master.zip
    Download_Files ${Download_Mirror}/nginx/memc-nginx-module-master.zip memc-nginx-module-master.zip
    unzip -o memc-nginx-module-master.zip
    unzip -o srcache-nginx-module-master.zip
    [[ ! -s /etc/init.d/memcached ]] && Install_Memcached 2
    cd ${cur_dir}/src
    Tar_Cd nginx-${Nginx_Version}.tar.gz nginx-${Nginx_Version}
    if echo ${Nginx_Version} | grep -Eqi '^[0-1].[5-8].[0-9]' || echo ${Nnginx_Version} | grep -Eqi '^1.9.[1-4]'; then
        ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${NginxMAOpt} ${Nginx_Modules_Options}
     else
        ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${NginxMAOpt} ${Nginx_Modules_Options}
    fi
    make
    mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.${Upgrade_Date}
    \cp objs/nginx /usr/local/nginx/sbin/nginx
    sed -i 's:^#memc::g' /usr/local/nginx/conf/nginx.conf
    for FileName in $(ls /usr/local/nginx/conf/vhost)
    do
        sed -i 's:^#memc::g' /usr/local/nginx/conf/vhost/${FileName}
    done
    echo "Test nginx configure file..."
    /usr/local/nginx/sbin/nginx -t
    echo "upgrade..."
    make upgrade
    Echo_Green "======== upgrade nginx with memc-nginx-module completed ======"
}

Uninstall_memc()
{
    echo "You will uninstall memc-nginx-module..."
    Nginx_Version=`/usr/local/nginx/sbin/nginx -v 2>&1 | cut -c22-`

    if [ -s /usr/local/include/jemalloc/jemalloc.h ] && /usr/local/nginx/sbin/nginx -V 2>&1|grep -Eqi 'ljemalloc'; then
        NginxMAOpt="--with-ld-opt='-ljemalloc'"
     elif [ -s /usr/local/include/gperftools/tcmalloc.h ] && grep -Eqi "google_perftools_profiles" /usr/local/nginx/conf/nginx.conf; then
        NginxMAOpt='--with-google_perftools_module'
     else
        NginxMAOpt=""
    fi
    echo "You will install nginx without memc-nginx-module ..."
    Press_Start
    cd ${cur_dir}/src
    if [ -s nginx-${Nginx_Version}.tar.gz ]; then
        echo "nginx-${Nginx_Version}.tar.gz [found]"
     else
        echo "Error: nginx-${Nginx_Version}.tar.gz not found!!!download now......"
        wget -c ${Download_Mirror}/nginx/nginx-${Nginx_Version}.tar.gz
        if [ $? -eq 0 ]; then
            echo "Download nginx-${Nginx_Version}.tar.gz successfully!"
         else
            wget -c http://nginx.org/download/nginx-${Nginx_Version}.tar.gz
            if [ $? -eq 0 ]; then
                echo "Download nginx-${Nginx_Version}.tar.gz successfully!"
             else
                echo "You enter Nginx Version was:"${Nginx_Version}
                Echo_Red "Error! You entered a wrong version number, please check!"
                sleep 5
                exit 1
            fi
        fi
    fi
    Tar_Cd nginx-${Nginx_Version}.tar.gz nginx-${Nginx_Version}
    if echo ${Nginx_Version} | grep -Eqi '^[0-1].[5-8].[0-9]' || echo ${Nnginx_Version} | grep -Eqi '^1.9.[1-4]'; then
        ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${NginxMAOpt} ${Nginx_Modules_Options}
     else
        ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${NginxMAOpt} ${Nginx_Modules_Options}
    fi
    make
    mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.${Upgrade_Date}
    \cp objs/nginx /usr/local/nginx/sbin/nginx
    sed -i 's:            srcache_:#memc            srcache_:g' /usr/local/nginx/conf/nginx.conf
    sed -i 's:            set $key $uri$args:#memc            set $key $uri$args:g' /usr/local/nginx/conf/nginx.conf
    for FileName in $(ls /usr/local/nginx/conf/vhost)
    do
        sed -i 's:            srcache_:#memc            srcache_:g' /usr/local/nginx/conf/vhost/${FileName}
        sed -i 's:            set $key $uri$args:#memc            set $key $uri$args:g' /usr/local/nginx/conf/vhost/${FileName}
    done
    echo "Test nginx configure file..."
    /usr/local/nginx/sbin/nginx -t
    echo "upgrade..."
    make upgrade
    Echo_Green "======== upgrade nginx without memc-nginx-module completed ======"
}